﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Dycox.ComponentModel
{
    public class RemoteTimeService : ITimeService
    {
        object _syncRoot = new object();
        Stopwatch _stopwatch;
        DateTime _start;

        public RemoteTimeService(DateTime serverTime)
        {
            _stopwatch = new Stopwatch();
            Synchronize(serverTime);
        }

        public DateTime Now
        {
            get
            {
                lock (_syncRoot)
                {
                    return _start + _stopwatch.Elapsed;
                }
            }
        }

        public void Synchronize(DateTime serverTime)
        {
            lock (_syncRoot)
            {
                _start = serverTime;
                _stopwatch.Restart();
            }
        }
    }
}
